/**
 * Created by Administrator on 2016/12/20 0020.
 */

'use strict';
var React = require('react-native');
var dbManager = React.NativeModules.DBManager;

class DBManager {

    /**
     * 本地保存数据,保存格式是字符串
     * @param key           String
     * @param value         String
     * @returns {Promise}
     */
    saveLocally(key, value) {
        return new Promise((fulfill, reject) => {
            dbManager.saveLocally(key, value, (error) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill();
                }
            });
        });
    }

    /**
     * 本地保存数据,保存格式是dictionary
     * @param key           String
     * @param value         Dictionary
     * @returns {Promise}
     */
    saveDictLocally(key, value) {
        return new Promise((fulfill, reject) => {
            dbManager.saveDictLocally(key, value, (error) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill();
                }
            });
        });
    }

    /**
     * 读取本地保存的数据
     * @param key           String
     * @returns {Promise}
     */
    readLocally(key) {
        return new Promise((fulfill, reject) => {
            dbManager.readLocally(key, (error, data) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill(data);
                }
            });
        });
    }


}
module.exports = new DBManager();